#!/usr/bin/env python
# -*- coding: utf-8 -*-


from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins
from rest_framework import viewsets

from apps.steps.models import Steps
from apps.steps.serializers import StepListSerializers, StepDetailSerializers, StepCreateSerializer, PreStepListSerializers
from utils.rest.views import CustomViewBase
from apps.steps.filters import StepFilter


class StepsPagination(PageNumberPagination):
    """
    测试项目列表自定义分页
    """

    # 默认每页显示的个数
    page_size = 10
    # 可以动态改变每页显示的个数
    page_size_query_param = 'limit'
    # 页码参数
    page_query_param = 'page'
    # 最多能显示多少页
    max_page_size = 100


class StepsViewSet(CustomViewBase):
    """
    测试操作步骤
    """

    queryset = Steps.objects.all().order_by('-step_num')
    # 分页
    # serializer_class = StepListSerializers
    filter_backends = (DjangoFilterBackend, )
    filter_class = StepFilter
    filter_fields = ('case',)
    ordering_fields = ('step', )
    pagination_class = StepsPagination
    # 搜索
    search_fields = ('name',)

    def get_serializer_class(self):
        if self.action == "list":
            return StepListSerializers
        elif self.action == "create":
            return StepCreateSerializer
        else:
            return StepDetailSerializers


class PreStepViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    前置操作步骤视图
    """
    queryset = Steps.objects.all().order_by('step_num')
    serializer_class = PreStepListSerializers
    filter_backends = (DjangoFilterBackend, )
    filter_class = StepFilter
    filter_fields = ('case',)
    pagination_class = StepsPagination
